﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Sidvall.AuthorizationManager.UI.Wpf.Converters
{
	public class BooleanToWindowStateConverter : IValueConverter
	{
		#region Public Members

		#region Inverted

		private bool _Inverted = false;
		public bool Inverted
		{
			get
			{
				return _Inverted;
			}
			set
			{
				_Inverted = value;
			}
		}

		#endregion
		#region Not

		private bool _Not = false;
		public bool Not
		{
			get
			{
				return _Not;
			}
			set
			{
				_Not = value;
			}
		}

		#endregion

		#region Convert

		public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return Inverted ? WindowStateToBool(value) : BoolToWindowState(value);
		}

		#endregion
		#region ConvertBack

		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
		{
			return Inverted ? BoolToWindowState(value) : WindowStateToBool(value);
		}

		#endregion

		#endregion
		#region Private Members

		#region WindowStateToBool

		private object WindowStateToBool(object value)
		{
			if (!(value is Xceed.Wpf.Toolkit.WindowState))
				return DependencyProperty.UnsetValue;
			return (((Xceed.Wpf.Toolkit.WindowState)value) == Xceed.Wpf.Toolkit.WindowState.Open) ^ Not;
		}

		#endregion
		#region BoolToWindowState

		private object BoolToWindowState(object value)
		{
			if (!(value is bool))
				return DependencyProperty.UnsetValue;
			return ((bool)value ^ Not) ? Xceed.Wpf.Toolkit.WindowState.Open : Xceed.Wpf.Toolkit.WindowState.Closed;
		}

		#endregion

		#endregion
	}
}
